package com.april.product.service.impl;

import com.april.common.utils.PageUtils;
import com.april.common.utils.Query;
import com.april.product.constant.GlobalConstant;
import com.april.product.converter.AttrGroupConverter;
import com.april.product.converter.CategoryConverter;
import com.april.product.entity.AttrGroupEntity;
import com.april.product.entity.Category;
import com.april.product.mapper.CategoryMapper;
import com.april.product.model.vo.AttrGroupVo;
import com.april.product.model.vo.CategoryVo;
import com.april.product.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<Category> page = this.page(
                new Query<Category>().getPage(params),
                new QueryWrapper<Category>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryVo> listWithTree() {
        List<CategoryVo> category = CategoryConverter.INSTANCES.toCategoryVo(baseMapper.selectList(null));
        return category.stream().filter(x -> x.getParentCid() == GlobalConstant.ROOT_ID)
                .peek(x -> x.setChildren(getChildren(x, category)))
                .sorted(Comparator.comparingInt(t -> (t.getSort() == null ? 0 : t.getSort()))).collect(Collectors.toList());

    }

    @Override
    public void removeMenuByIds(List<Long> list) {
        //TODO 检查当前删除的菜单，是否被别的地方引用
        baseMapper.deleteBatchIds(list);
    }

    @Override
    public AttrGroupVo findCateLogPath(AttrGroupEntity attrGroup) {
        AttrGroupVo attrGroupVo = AttrGroupConverter.INSTANCES.toAttrGroupVo(attrGroup);
        List<Long> paths = new ArrayList<>();
        // List<Long> parentPath = findParentPath(attrGroupVo.getCatelogId(), paths).stream().sorted(Comparator.reverseOrder())
        //         .collect(Collectors.toList());
        //

        List<Long> parentPath = findParentPath(attrGroupVo.getCatelogId(), paths);
        Collections.reverse(parentPath);
        attrGroupVo.setCatelogPath((parentPath.toArray(new Long[0])));
        return attrGroupVo;
    }

    private List<Long> findParentPath(Long cateLogId,List<Long> paths) {
        paths.add(cateLogId);
        Category byId = this.getById(cateLogId);
        if (byId != null && byId.getParentCid() != 0) {
            findParentPath(byId.getParentCid(),paths);
        }
        return paths;
    }

    private List<CategoryVo> getChildren(CategoryVo menu, List<CategoryVo> allMenu) {
        return allMenu.stream().filter(x -> Objects.equals(x.getParentCid(), menu.getCatId()))
                .peek(x -> x.setChildren(getChildren(x,allMenu)))
                .sorted(Comparator.comparingInt(t -> (t.getSort() == null ? 0 : t.getSort())))
                .collect(Collectors.toList());
        // allMenu.stream().filter(x -> x.getParentCid() == menu.getCatId())
        //         .map(categoryVo -> {
        //             categoryVo.setChildren(getChildren(categoryVo,allMenu));
        //             return categoryVo;
        //         }).sorted((t1,t2) -> (t1.getSort() == null ? 0 :t1.getSort())- (t2.getSort() == null ? 0 :t2.getSort()))
        //         .collect(Collectors.toList());
    }

}